﻿using System;
using System.Windows.Forms;
using System.Xml;

using Core;
using ClassLibrary;

namespace TestProject
{
    public delegate void MyDeleg (string str);
    public partial class NewNatSelTable : Form
    {
        private Model model;
        private NaturalSelectionLink def_natsel = null;
        public string def_natsel_path=null,def_reprod_path=null;
        public NewNatSelTable(Model m)
        {
            InitializeComponent();
            model = m;
        }
        private void func1(string str)
        {
            def_natsel_path = str;
        }
        private void func2(string str)
        {
            def_reprod_path = str;
        }
        private void create_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Tables\\NaturalSelectionTables\\" + name_box.Text + ".xml"))
            {
                MessageBox.Show("A natural selection table with this name aleady exists. Please, choose another name.");
                return;
            }
            PhenotypeTable phen_t = null;
            ReproductionLink def_reprod = null;
            foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
            {
                if (t.name == (string)phen_t_box.SelectedItem)
                {
                    phen_t = t;
                    break;
                }
            }
            foreach (ReproductionLink link in model.model_reproduction_links.Values)
            {
                if (link.name == (string)def_reprod_box.SelectedItem)
                {
                    def_reprod = link;
                    def_reprod_path = def_reprod.path;
                    break;
                }
            }
            if (checkBox1.Checked)
            {
                NewNatSelLink link = new NewNatSelLink(model, phen_t.name, phen_t.path, phen_t.characteristics_amount, new MyDeleg(func1),new MyDeleg(func2));
                link.Owner = this;
                if (link.ShowDialog() != DialogResult.OK)
                {
                    MessageBox.Show("Default links were not created. Try again.");
                    return;
                }
            }
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Tables\\NaturalSelectionTables\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("table");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("PathToPhenotypeTable", phen_t.path);
            writer.WriteAttributeString("NaturalSelectionVariantsAmount", amount_box.Value.ToString());
            writer.WriteAttributeString("PathToDefualtNaturalSelectionLink", def_natsel_path);
            writer.WriteAttributeString("PathToDefualtReproductionLink", def_reprod_path);
            if ((amount_box.Text != null) && (amount_box.Text != "0"))
            {
                for (int i = 0; i < Convert.ToInt32(amount_box.Text); i++)
                {
                    writer.WriteStartElement("natural_selection_variant");
                    writer.WriteAttributeString("Id", i.ToString());
                    writer.WriteAttributeString("FirstTimeStep", "0");
                    writer.WriteAttributeString("LastTimeStep", "0");
                    writer.WriteAttributeString("AmountIndividualsLowerBound", "0");
                    writer.WriteAttributeString("AmountIndividualsUpperBound", "0");
                    writer.WriteAttributeString("PathToNaturalSelectionLink", "");
                    writer.WriteAttributeString("PathToReproductionLink", "");
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
            XmlElement el;
            el = doc.CreateElement("table");
            el.SetAttribute("Path", "file_input\\" + model.model_name + "\\Tables\\NaturalSelectionTables\\" + name_box.Text + ".xml");
            doc.Increment(doc.GetElementsByTagName("tables")[0].Attributes[3]);
            doc.Increment(doc.DocumentElement.Attributes[1]);
            doc.GetElementsByTagName("natural_selection_tables")[0].AppendChild(el);
            doc.Save(model.model_path);
            Functions.add_node_func(new NaturalSelectionTable("file_input\\" + model.model_name + "\\Tables\\NaturalSelectionTables\\" + name_box.Text + ".xml"), name_box.Text, "natselt");
            this.Close();
        }

        private void canc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void def_natsel_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                if (link.name == (string)def_natsel_box.SelectedItem)
                {
                    def_natsel = link;
                    def_natsel_path = def_natsel.path;
                    break;
                }
            }
            def_reprod_box.Items.Clear();
            foreach (ReproductionLink link in model.model_reproduction_links.Values)
            {
                if (link.path_to_natsel_link == def_natsel.path)
                    def_reprod_box.Items.Add(link.name);
            }
            def_reprod_box.SelectedIndex = 0;
        }

        private void NewNatSelTable_Load(object sender, EventArgs e)
        {
            foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
            {
                phen_t_box.Items.Add(t.name);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                def_reprod_box.Enabled = false;
                def_natsel_box.Enabled = false;
            }
            else
            {
                def_reprod_box.Enabled = true;
                def_natsel_box.Enabled = true;
            }
        }

        private void phen_t_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            def_natsel_box.Items.Clear();
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                def_natsel_box.Items.Add(link.name);
            }
            def_natsel_box.SelectedItem = null;
        }
    }
}
